﻿using CommunityToolkit.Mvvm.Input;
using HslCommunication.Profinet.Siemens;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLinePacking.Common;
using ZoneLinePacking.Core;
using ZoneLinePacking.Enums;
using ZoneLinePacking.Models;
using ZoneLinePacking.Models.Dto;
using ZoneLinePacking.Models.Entity;
using ZoneLinePacking.Models.Entity.BaseInfo;
using ZoneLinePacking.Models.Package;
using ZoneLinePacking.Models.StationConfig;
using ZoneLinePacking.Models.StationResult.HOPStation;
using ZoneLinePacking.Models.StationResult.QOPStation;
using ZoneLinePacking.Services.DB;
using ZoneLinePacking.Services.Message;
using ZoneLinePacking.ViewModelBase;

namespace ZoneLinePacking.ViewModels.Dialogs {

    public class SelectProductDialogViewModel : DialogViewModel {


        private readonly IDbOperate<IFreeSqlMainContext> _mainDbOperate = null;
        public SelectProductDialogViewModel(
            IRegionManager regionManager,
            IDialogService dialog, 
            IEventAggregator eventAggregator, 
            IMessageService message, 
            IDbOperate<IFreeSqlMainContext> mainDbOperate) 
            : base(regionManager, dialog, eventAggregator, message, mainDbOperate) {

            this._mainDbOperate = mainDbOperate;
           
            //加载客户列表
            var clist = base._mainDbOperate.GetModelList<CustomerEntity>(o => true);
            CustomerTypes.Clear();
            CustomerTypes.AddRange(clist);
        }

        #region Props
        private ObservableCollection<CustomerEntity> customerTypes = new ObservableCollection<CustomerEntity>();
        /// <summary>
        /// 客户列表
        /// </summary>
        public ObservableCollection<CustomerEntity> CustomerTypes {
            get { return customerTypes; }
            set { customerTypes = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<ProductDto> productionTypes = new ObservableCollection<ProductDto>();
        /// <summary>
        /// 产品列表
        /// </summary>
        public ObservableCollection<ProductDto> ProductionTypes {
            get { return productionTypes; }
            set { productionTypes = value; RaisePropertyChanged(); }
        }
        #endregion


        #region Commands

        public DelegateCommand<CustomerEntity> CustomerTypeSelectedCmd => new DelegateCommand<CustomerEntity>((rhs) => {

            if (rhs != null) {
                var plist = _mainDbOperate.GetFreeSql().Select<ProductEntity, CustomerEntity>()
                 .LeftJoin((a, b) => a.CustomerID == b.Id)
                 .Where((a, b) => a.CustomerID == rhs.Id)
                 .ToList((a, b) => new ProductDto {
                     ProductName = a.ProductName,
                     Description = a.Description,
                     Id = a.Id,
                     Model = a.Model,
                     Memo = a.Memo,
                     WorkDirection = a.WorkDirection,
                     MaterialNumber = a.MaterialNumber,
                     Unit = a.Unit,
                     CustomerID = b.Id,
                     CustomerName = b.Name,
                 });
                ProductionTypes.Clear();
                ProductionTypes.AddRange(plist);
                Cache.CurWorkProduct = null;
                Cache.CurrentFormula.Clear();
            }
        });

        public DelegateCommand<ProductDto> ProductionTypeSelectedCmd => new DelegateCommand<ProductDto>((rhs) => {
            if (rhs == null) {
                return;
            }
            Cache.CurWorkProduct = rhs;
            Cache.CurrentFormula.Clear();
        });

        public DelegateCommand StartCommand => new DelegateCommand(() => {

            if (Cache.CurWorkProduct == null) {
                _message.Warning("请选择装箱产品！");
                return;
            }

            var curInPackageTempList = _mainDbOperate.GetModelList<PackageTempModel>(p => true);
            string productStr = $"{curInPackageTempList.Select(o => o.Brand).Distinct().FirstOrDefault()}{curInPackageTempList.Select(o => o.Model).Distinct().FirstOrDefault()}{curInPackageTempList.Select(o => o.LeftRightLable).Distinct().FirstOrDefault()}";
            if (curInPackageTempList.Any(o => o.Brand != Cache.CurWorkProduct.CustomerName)) {
                _message.Error($"当前列表中有{curInPackageTempList.Count}个【{productStr}】产品与选择的装箱产品【品牌不匹配】！");
                return;
            }

            if (curInPackageTempList.Any(o => !string.IsNullOrEmpty(o.Model) && o.Model != Cache.CurWorkProduct.Model)) {
                _message.Error($"当前列表中有{curInPackageTempList.Count}个【{productStr}】产品与选择的装箱产品【型号不匹配】！");
                return;
            }

            if (curInPackageTempList.Any(o => o.LeftRight != (int)Cache.CurWorkProduct.WorkDirection)) {
                _message.Error($"当前列表中有{curInPackageTempList.Count}个【{productStr}】产品与选择的装箱产品【左右方向不匹配】！");
                return;
            }

            //验证通过
            base.OnDialogClosed(ButtonResult.OK);
        });
        #endregion


    }


}
